﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NewsMine.Logging;
using NewsMine.Utilities;

namespace WebApp1.Controllers
{
    public class BaseController : Controller
    {
        static INewsMineLogger _logger = NewsMineLogManager.GetLogger("BaseController");

        public BaseController()
        {
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            System.IO.File.AppendAllText(@"c:\\webSiteBaseControllerExceptions.txt",Environment.StackTrace);

            if (filterContext!=null && filterContext.Exception != null)
            {
                System.IO.File.AppendAllText(@"c:\\webSiteBaseControllerExceptions.txt", Environment.NewLine + " ---------------" + Environment.NewLine +  filterContext.Exception.Message + Environment.NewLine +
                "url is : " + Request.Url.AbsolutePath + Environment.NewLine +
                "Query is : " + Request.Url.Query + Environment.NewLine +
                Environment.StackTrace + Environment.NewLine + filterContext.Exception.StackTrace);

                TempLogger.LogCustom("webSiteBaseControllerExceptions.txt",

                filterContext.Exception.Message + Environment.NewLine +
                "url is : " + Request.Url.AbsolutePath + Environment.NewLine +
                "Query is : " + Request.Url.Query + Environment.NewLine +
                Environment.StackTrace + Environment.NewLine + filterContext.Exception.StackTrace);

                _logger.ErrorException("Error in base controller. " + filterContext.Exception.Message, filterContext.Exception);
            
            }

            base.OnException(filterContext);
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //handling the user.
            //Check for the user related cookie in the request, if there is just go with that. if not issue the guest user token in the user's response cookie.

            base.OnActionExecuting(filterContext);
        }

        private void BuildUserContext()
        {
            // Get the user id from the cookie.
            // Get the user's favorite tags and also other meta data around that perticular user.
            // Get the current user visiting behaviour like page number, language, tags and search  query, category and also the geography information..
            // Get the current running website.... And also we can have most recent items visited by user.


            // By looking at the url, we should be able to get the basic context like, current language and other parameters.
            // we can get user information also from the url or cookie.
            // page number also can be taken from the url 
            // website, channel, tag , category, menu text can be accessed from the url.
            
            // domain/source/ndtv.com
            // domain/source-


        }

       
    }
}
